package com.own.stu.apm.agent.core.plugin.loader;

/**
 * 用于加载插件里面的拦截器
 */
public class InterceptorInstanceLoader {

    /**
     *
     * 要想在插件拦截器中能够访问到被拦截的类需要是同一个类加载器或子类类加载器被拦截的类:A-C1
     *
     * @param interceptorName
     * @param targetClassLoader
     * @param <T>
     * @return
     */
    public static <T> T load(String interceptorName, ClassLoader targetClassLoader)
            throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        if (targetClassLoader == null) {
            targetClassLoader = InterceptorInstanceLoader.class.getClassLoader();
        }
        AgentClassLoader classLoader = new AgentClassLoader(targetClassLoader);
        Object o = Class.forName(interceptorName, true, classLoader).newInstance();
        return (T) o;
    }
}
